package www.yingdi365.com.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseFragment;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.AppUser;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.LogUtil;
import www.yingdi365.com.util.ScreenUtils;
import www.yingdi365.com.viewholder.WoViewHolder;

/**
 * A simple {@link Fragment} subclass.
 */
public class WoFragment extends ZjbBaseFragment {

    private RecyclerArrayAdapter<AppUser> adapter;
    private EasyRecyclerView recyclerView;
    private View mInflate;
    private View mRelaTitleStatue;

    public WoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (mInflate == null) {
            mInflate = inflater.inflate(R.layout.fragment_wo, container, false);
            init();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mInflate.getParent();
        if (parent != null) {
            parent.removeView(mInflate);
        }
        return mInflate;
    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void findID() {
        mRelaTitleStatue = mInflate.findViewById(R.id.relaTitleStatue);
        recyclerView = (EasyRecyclerView) mInflate.findViewById(R.id.recyclerView);
    }

    @Override
    protected void initViews() {
        mRelaTitleStatue.setPadding(0, ScreenUtils.getStatusBarHeight(getActivity()), 0, 0);
        initRecycle();
    }

    @Override
    protected void setListeners() {
    }

    @Override
    protected void initData() {
        onRefresh();
    }

    private void initRecycle() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter<AppUser>(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                int layout = R.layout.view_wo;
                return new WoViewHolder(parent, layout);
            }
        });
    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.APPUSERSERVICE + "user";
        HttpHeaders headers = new HttpHeaders();
        headers.put(Constant.TOKEN, appLoginResult.getToken());
//        headers.put("TYPE", "ANDROID");
//        headers.put("VERSION", VersionUtils.getCurrVersion(getContext())+"");
        LogUtil.LogShitou("WoFragment--getOkObject", appLoginResult.getToken());
        HashMap<String, String> params = new HashMap<>();
        return new OkObject(headers, params, url);
    }

    public void onRefresh() {
        OkObject okObject = getOkObject();
        OkGo.get(okObject.getUrl())//
                .tag(this)//
                .headers(okObject.getHeaders())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.LogShitou("个人信息", s);
                        try {
                            AppUser appUser = GsonUtils.parseJSON(s, AppUser.class);
                            adapter.add(appUser);
                            adapter.notifyDataSetChanged();
                        } catch (Exception e) {
                            showError(getResources().getString(R.string.shujuchucuo));
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        if (response != null) {
                            if (response.code() == 401) {
                                showError("Token失效");
                                showReLoginDialog();
                            } else if (response.code() == 403) {
                                showError("有新版本");
                            } else {
                                showError(getResources().getString(R.string.wangluochucuo));
                            }
                        } else {
                            showError(getResources().getString(R.string.wangluochucuo));
                        }
                    }

                    public void showError(String msg) {
                        View view_loaderror = LayoutInflater.from(getActivity()).inflate(R.layout.view_loaderror, null);
                        TextView textMsg = (TextView) view_loaderror.findViewById(R.id.textMsg);
                        textMsg.setText(msg);
                        view_loaderror.findViewById(R.id.buttonReLoad).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                initData();
                            }
                        });
                        recyclerView.setErrorView(view_loaderror);
                        recyclerView.showError();
                    }
                });
    }
}
